package com.foolishbird.sdk.core;

import android.Manifest;
import android.text.TextUtils;

import com.foolishbird.sdk.api.FBSDK;
import com.foolishbird.sdk.api.WanSDKConfiguration;
import com.foolishbird.sdk.handler.SimpleAsyncResponseHandler;
import com.foolishbird.sdk.params.BaseRequestParams;
import com.foolishbird.sdk.params.BindIDCardReuqestParams;
import com.foolishbird.sdk.params.BindPhoneReuqestParams;
import com.foolishbird.sdk.params.ConfigInfoRequestParams;
import com.foolishbird.sdk.params.CreateOrderRequestParams;
import com.foolishbird.sdk.params.FloatChangePhoneParams;
import com.foolishbird.sdk.params.FloatChangePwdByPhoneParams;
import com.foolishbird.sdk.params.FloatChangePwdByPwdParams;
import com.foolishbird.sdk.params.FloatCommonReuqestParams;
import com.foolishbird.sdk.params.FloatGiftReciveRequestParams;
import com.foolishbird.sdk.params.FloatPhoneCodeParams;
import com.foolishbird.sdk.params.GetRequestParams;
import com.foolishbird.sdk.params.LoginAutoRequestParams;
import com.foolishbird.sdk.params.LoginPhoneRequestParams;
import com.foolishbird.sdk.params.LoginRequestParams;
import com.foolishbird.sdk.params.LogoutRequestParams;
import com.foolishbird.sdk.params.ModifyPasswordReuqestParams;
import com.foolishbird.sdk.params.PayChannelReuqestParams;
import com.foolishbird.sdk.params.QuickRegisterRequestParams;
import com.foolishbird.sdk.params.RedAlipayRequestParams;
import com.foolishbird.sdk.params.RedBindedDeleteRequestParams;
import com.foolishbird.sdk.params.RedBindedRequestParams;
import com.foolishbird.sdk.params.RedCheckRequestParams;
import com.foolishbird.sdk.params.RedDetailRequestParams;
import com.foolishbird.sdk.params.RedGetRequestParams;
import com.foolishbird.sdk.params.RedInfoRequestParams;
import com.foolishbird.sdk.params.RedMainRequestParams;
import com.foolishbird.sdk.params.RedOpenRequestParams;
import com.foolishbird.sdk.params.RedSendRequestParams;
import com.foolishbird.sdk.params.RedTixianRequestParams;
import com.foolishbird.sdk.params.RedUserInfoRequestParams;
import com.foolishbird.sdk.params.RedWechatRequestParams;
import com.foolishbird.sdk.params.RegisterRequestParams;
import com.foolishbird.sdk.params.RoleInfoRequestParams;
import com.foolishbird.sdk.params.SendVerifyReuqestParams;
import com.foolishbird.sdk.params.bean.FBDeviceInfo;
import com.foolishbird.sdk.params.bean.GameInfo;
import com.foolishbird.sdk.params.bean.Goods;
import com.foolishbird.sdk.params.bean.PayType;
import com.foolishbird.sdk.params.bean.RedUserInfo;
import com.foolishbird.sdk.params.bean.RoleInfo;
import com.foolishbird.sdk.params.bean.RoleStatus;
import com.foolishbird.sdk.params.bean.ValidationInfo;
import com.foolishbird.sdk.params.bean.VerifyType;
import com.foolishbird.sdk.secret.MD5;
import com.foolishbird.sdk.system.DeviceInfo;
import com.foolishbird.sdk.utils.AndroidDeviceGenerator;
import com.foolishbird.sdk.utils.AndroidEmulatorChecker;
import com.foolishbird.sdk.utils.CheckRoot;
import com.foolishbird.sdk.utils.ToolsUtils;
import com.foolishbird.sdk.utils.WL;
import com.foolishbird.sdk.utils.XmlUtils;


public final class HttpEngineApi {

    private static final String TAG = HttpEngineApi.class.getSimpleName();

    private volatile static HttpEngineApi instance;
    private HttpEngineConduits engineConduits;

    WanSDKConfiguration configuration;

    HttpEngine httpEngine;

    public static HttpEngineApi getInstance() {
        if (instance == null) {
            synchronized (HttpEngine.class) {
                if (instance == null) {
                    instance = new HttpEngineApi();
                }
            }
        }
        return instance;
    }

    public synchronized void init(WanSDKConfiguration configuration) {
        if (configuration == null) {
            throw new IllegalArgumentException("HttpEngineApi configuration can not be initialized with null");
        }

        if (this.configuration == null) {
            this.configuration = configuration;
        } else {
            WL.w(TAG, "Try to initialize HttpEngineApi which had already been initialized before. To re-init HttpEngineApi with new configuration call HttpEngineApi.destroy() at first.");
        }

        if (httpEngine == null) {
            httpEngine = new HttpEngine(configuration.getThreadPoolSize());
        } else {
            WL.w(TAG, "Try to initialize HttpEngineApi which had already been initialized before. To re-init HttpEngineApi with new configuration call HttpEngineApi.destroy() at first.");
        }

        engineConduits = new HttpEngineConduits(configuration.getApplicationContext());
        engineConduits.saveGameId(configuration.getGameId());
    }

    public synchronized void destroy() {
        if (this.configuration != null) {
            this.configuration = null;
            WL.d(TAG, "Destroy HttpEngine configuration");
        }

        if (httpEngine != null && !httpEngine.isShutdown()) {
            httpEngine.shutdown();
            httpEngine = null;
        }
    }

    private String getBaseHttpPostUrl(){
        return XmlUtils.getPlatformPostUrl();
    }

    private String getBaseHttpGetUrl(){
        return XmlUtils.getPlatformGetUrl();
    }

    public void cancel(int taskId) {
        if (httpEngine != null) {
            httpEngine.cancelTask(taskId);
        }
    }

    public boolean isLoggedIn() {
        return engineConduits == null ? false : engineConduits.isLoggedIn();
    }

    static class HttpEngineInitializeException extends RuntimeException {
        public HttpEngineInitializeException(String message) {
            super(message);
        }
    }

    void checkEngineInitialization() {
        if (httpEngine == null || httpEngine.isShutdown() || configuration == null) {
            throw new HttpEngineInitializeException("The HTTP engine is not initialized.");
        }
    }

    public HttpEngineConduits getEngineConduits() {
        return engineConduits;
    }

    public int getPlatformConfig(SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        ConfigInfoRequestParams params = new ConfigInfoRequestParams();
        setExtendedParams(params);

        return httpEngine.getPlatformConfig(getBaseHttpPostUrl(), params, new HttpEngineInterceptor(handler));
    }



    public int loginByVerify(String username, String verify, SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        LoginPhoneRequestParams params = new LoginPhoneRequestParams(username, verify);
        setExtendedParams(params);


        return httpEngine.loginByPhone(getBaseHttpPostUrl(), params, new HttpEngineInterceptor(handler));
    }

//    private void setExtendedDefaultParams(BaseRequestParams params){
//        FBDeviceInfo info = new FBDeviceInfo();
//        info.setAllApp("屏幕录制,小米直播助手,小米社区,王者荣耀,交管12123,美团,优酷视频,北京医院挂号网,大众点评,三方应用异常分析,录音机,百度输入法小米版,国家反诈中心,微信,天星金融,系统语音引擎,福利影音,虎牙直播,淘特,应用宝,小米换机,梅花视频,百度,健康,小米文档查看器（WPS定制）,晋享生活,小米画报,58同城,意见反馈,全球上网,指南针,东方财富,腾讯视频,万能遥控,QQ浏览器,拼多多,今日头条,芒果TV,Google通讯录同步,UC浏览器,小米商城,抖音,QQ,狂神无双,酷狗音乐,讯飞输入法小米版,云闪付,内容中心,笔记,WiFi万能钥匙,儿童模式,喜马拉雅,百度地图,健康山西,计算器,平安好车主,中油好客e站,小米有品,游戏中心,番茄免费小说,去哪儿旅行,一键锁屏,垃圾清理,天气,高德地图,扫一扫,阅读,携程旅行,电子邮件,微博,智慧生活,智能出行,快手,狐狸视频,邮储银行,淘宝,米家,驾车场景,上汽大众,小米云盘,支付宝,夜半晚报,WPS Office,唯品会");
//        info.setAndroidId("7d4ec1f548f0d536");
//        info.setAvailMemory("2.18 GB");
//        info.setBrand("Xiaomi");
//        info.setIsAndroidEmulator("2");
//        info.setIsRoot("1");
//        info.setMac("8C:AA:CE:97:8D:2E");
//        info.setModel("M2102J2SC");
//        info.setTotalMemory("7.98 GB");
////        if(!TextUtils.isEmpty(engineConduits.readCurrentOaid())){
////            info.setDeviceOaid(engineConduits.readCurrentOaid());
////        }
//        info.setDeviceOaid(null);
////        if (ToolsUtils.checkAppSelfPermission(configuration.getApplicationContext(), Manifest.permission.READ_PHONE_STATE)) {
//            info.setImei("unknown");
//            info.setLineNumber("unknow");
//
////        }
////        info.setSystemVersion(android.os.Build.VERSION.RELEASE);
//        params.getBaseFB().setDeviceInfo(info);
//
//        GameInfo gameInfo = new GameInfo();
//        gameInfo.setGameId("19");
//        String ttChannel = WanSDK.getInstance().getTouTiaoChannel();
////        if(TextUtils.isEmpty(ttChannel)){
////            gameInfo.setChannelId(XmlUtils.getSdkChannelId());
////        }else {
////            gameInfo.setChannelId("174");
////        }
//        gameInfo.setChannelId("174");
//        gameInfo.setSdkVersion(XmlUtils.getSdkVersionName());
//        if(roleInfo!=null){
//            gameInfo.setRoleId(roleInfo.getRoleId());
//            gameInfo.setRoleName(roleInfo.getRoleName());
//            gameInfo.setServerId(roleInfo.getServerId());
//            gameInfo.setServerName(roleInfo.getServerName());
//        }
//        params.getBaseFB().setGameInfo(gameInfo);
//
//
//        ValidationInfo validationInfo = new ValidationInfo();
//        validationInfo.setService(params.getServiceString());
//        validationInfo.setToken(" ");
//        validationInfo.setTime("1659000475");
//        params.getBaseFB().setValidationInfo(validationInfo);
//    }



    private void setExtendedParams(BaseRequestParams params){
        FBDeviceInfo info = new FBDeviceInfo();
        info.setAllApp(DeviceInfo.getAllApp(configuration.getApplicationContext()));
        info.setAndroidId(AndroidDeviceGenerator.getAndroidId(configuration.getApplicationContext()));
        info.setAvailMemory(DeviceInfo.getAvailMemory(configuration.getApplicationContext()));
        info.setBrand(android.os.Build.BRAND);
        info.setIsAndroidEmulator(AndroidEmulatorChecker.isAndroidEmulator()?"1":"2");
        info.setIsRoot(CheckRoot.isDeviceRooted()?"2":"1");
        info.setMac(DeviceInfo.getMacAddress(configuration.getApplicationContext()));
        info.setModel(android.os.Build.MODEL);
        info.setTotalMemory(DeviceInfo.getTotalMemory(configuration.getApplicationContext()));
        info.setDeviceStr(AndroidDeviceGenerator.getDeviceID(configuration.getApplicationContext()));
        info.setDeviceId(MD5.encrypt(AndroidDeviceGenerator.getDeviceID(configuration.getApplicationContext())));
        if(!TextUtils.isEmpty(engineConduits.readCurrentOaid())){
            info.setDeviceOaid(engineConduits.readCurrentOaid());
        }
        if (ToolsUtils.checkAppSelfPermission(configuration.getApplicationContext(), Manifest.permission.READ_PHONE_STATE)) {
            info.setImei(AndroidDeviceGenerator.getAndroidDeviceId(configuration.getApplicationContext()));
            info.setLineNumber(AndroidDeviceGenerator.getLine1Number(configuration.getApplicationContext()));

        }

//        info.setSystemVersion(android.os.Build.VERSION.RELEASE);
        params.getBaseFB().setDeviceInfo(info);

        GameInfo gameInfo = new GameInfo();
        gameInfo.setGameId(configuration.getGameId());
        String ttChannel = FBSDK.getInstance().getTouTiaoChannel();
        if(TextUtils.isEmpty(ttChannel)){
            gameInfo.setChannelId(XmlUtils.getSdkChannelId());
        }else {
            gameInfo.setChannelId(ttChannel);
        }
        gameInfo.setSdkVersion(XmlUtils.getSdkVersionName());
        if(roleInfo!=null){
            gameInfo.setRoleId(roleInfo.getRoleId());
            gameInfo.setRoleName(roleInfo.getRoleName());
            gameInfo.setServerId(roleInfo.getServerId());
            gameInfo.setServerName(roleInfo.getServerName());
        }
        params.getBaseFB().setGameInfo(gameInfo);


        ValidationInfo validationInfo = new ValidationInfo();
        validationInfo.setService(params.getServiceString());
        validationInfo.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        validationInfo.setTime(System.currentTimeMillis() / 1000+"");
        params.getBaseFB().setValidationInfo(validationInfo);
    }



    @Deprecated
    public int login(String username, String password, SimpleAsyncResponseHandler handler) {
        return login(username, password, false, handler);
    }

    /**
     * 登录接口
     * @param username 登录帐号名称
     * @param password 如果为自动登录，即isAutoLogin参数为true，密码需要为上一次登录返回结果的password加密数据
     * @param isAutoLogin 是否为自动登录
     * @param handler
     * @return
     */
    public int login(String username, String password, boolean isAutoLogin, SimpleAsyncResponseHandler handler) {
        checkEngineInitialization();
        if(isAutoLogin||password.length()==32){
            LoginAutoRequestParams params = new LoginAutoRequestParams();
            setExtendedParams(params);
            return httpEngine.autoLogin(getBaseHttpPostUrl(), params, new HttpEngineInterceptor(handler));
        }else{
            LoginRequestParams params = new LoginRequestParams(username, password);
            setExtendedParams(params);
            return httpEngine.login(getBaseHttpPostUrl(), params, new HttpEngineInterceptor(handler));
        }

    }

    public int quickRegister(SimpleAsyncResponseHandler handler) {
        checkEngineInitialization();
        QuickRegisterRequestParams params = new QuickRegisterRequestParams();
        setExtendedParams(params);
        return httpEngine.quickRegister(getBaseHttpPostUrl(), params, new HttpEngineInterceptor(handler));
    }

    public int register(String username, String password, SimpleAsyncResponseHandler handler) {
        checkEngineInitialization();
        RegisterRequestParams params = new RegisterRequestParams(username, password);
        setExtendedParams(params);
//        RegisterRequestParams params = new RegisterRequestParams("369147987", "liyan8569922 .");
//        setExtendedDefaultParams(params);
        return httpEngine.register(getBaseHttpPostUrl(), params, new HttpEngineInterceptor(handler));
    }

    public int logout(SimpleAsyncResponseHandler handler) {
        checkEngineInitialization();
        LogoutRequestParams params = new LogoutRequestParams();
        setExtendedParams(params);
//        params.setDeviceId(AndroidDeviceGenerator.getDeviceID(configuration.getApplicationContext()));
//        params.setGameId(configuration.getGameId());
//        params.setUserId(engineConduits.readCurrentLoginUserUID());
//        params.setTicket(engineConduits.readCurrentLoginUserTicket());
        httpEngine.logout(getBaseHttpPostUrl(), params, new HttpEngineInterceptor(handler));
        engineConduits.isLoggedIn = false;
//        engineConduits.clearRealnameInfo();
//        engineConduits.saveCurrentLoginUserTickit("");
//        engineConduits.saveCurrentLoginUserUID("");
        return Integer.MAX_VALUE;
    }

    public int bindPhoneNumber(String cellphoneNumber, String verify, SimpleAsyncResponseHandler handler) {
        return bindPhoneNumber(cellphoneNumber, verify, null, null, handler);
    }

    public int bindPhoneNumber(String cellphoneNumber, String verify, String realname, String idCard, SimpleAsyncResponseHandler handler) {
        if(TextUtils.isEmpty(idCard)){
            checkEngineInitialization();
            String uid = engineConduits.readCurrentLoginUserUID();
            BindPhoneReuqestParams params = new BindPhoneReuqestParams(cellphoneNumber, uid, verify);
            setExtendedParams(params);
//        params.setGameId(configuration.getGameId());
//        params.setUserId(uid);
//        params.setDeviceId(AndroidDeviceGenerator.getDeviceID(configuration.getApplicationContext()));
//        params.setTicket(engineConduits.readCurrentLoginUserTicket());
//        params.setRealname(realname);
//        params.setIdcard(idCard);
            return httpEngine.bindPhoneNumber(getBaseHttpPostUrl(), params, new HttpEngineInterceptor(handler));
        }else{
            return  bindIDcard(realname,idCard,handler);
        }
    }

    public int bindIDcard(String realname, String idCard, SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        String uid = engineConduits.readCurrentLoginUserUID();
        BindIDCardReuqestParams params = new BindIDCardReuqestParams(realname, idCard);
        setExtendedParams(params);
//        params.setGameId(configuration.getGameId());
//        params.setUserId(uid);
//        params.setDeviceId(AndroidDeviceGenerator.getDeviceID(configuration.getApplicationContext()));
//        params.setTicket(engineConduits.readCurrentLoginUserTicket());
//        params.setRealname(realname);
//        params.setIdcard(idCard);
        return httpEngine.bindIDCard(getBaseHttpPostUrl(), params, new HttpEngineInterceptor(handler));
    }

    public int sendPhoneVerifyCode(String verify, VerifyType verifyType, SimpleAsyncResponseHandler handler) {
        checkEngineInitialization();
        SendVerifyReuqestParams params;
        if (VerifyType.PHONE_VERIFY == verifyType) {
            params = new SendVerifyReuqestParams(verify);
//            params.setTicket(engineConduits.readCurrentLoginUserTicket());
//            params.setUserId(engineConduits.readCurrentLoginUserUID());
        } else if (VerifyType.USERNAME_VERIFY == verifyType) {
            params = new SendVerifyReuqestParams(verify, configuration.getGameId());
//            params.setTicket(engineConduits.readCurrentLoginUserTicket());
//            params.setUserId(engineConduits.readCurrentLoginUserUID());
        } else if (VerifyType.LOGIN_VERIFY == verifyType) {
            params = new SendVerifyReuqestParams(verify, VerifyType.LOGIN_VERIFY);
        } else {
            params = new SendVerifyReuqestParams(verify,verifyType);
        }
        setExtendedParams(params);
//        params.setGameId(configuration.getGameId());
//        params.setDeviceId(AndroidDeviceGenerator.getDeviceID(configuration.getApplicationContext()));
        return httpEngine.sendPhoneVerify(getBaseHttpPostUrl(), params, new HttpEngineInterceptor(handler));
    }

    public int modifyPassword(String username, String password, String verify, SimpleAsyncResponseHandler handler) {
        checkEngineInitialization();
        ModifyPasswordReuqestParams params = new ModifyPasswordReuqestParams(username,verify, password);

        setExtendedParams(params);
//        params.setGameId(configuration.getGameId());
//        params.setUserId(engineConduits.readCurrentLoginUserUID());
//        params.setDeviceId(AndroidDeviceGenerator.getDeviceID(configuration.getApplicationContext()));
//        params.setTicket(engineConduits.readCurrentLoginUserTicket());
        return httpEngine.modifyPassword(getBaseHttpPostUrl(), params, new HttpEngineInterceptor(handler));
    }

    public int getPayChannelInfo(Goods goods, SimpleAsyncResponseHandler handler) {
        checkEngineInitialization();
        PayChannelReuqestParams params = new PayChannelReuqestParams();
        params.getExtendedFB().setPayAmount(goods.getGoodsPrice());

        setExtendedParams(params);
//        params.setUserId(engineConduits.readCurrentLoginUserUID());
//        params.setDeviceId(AndroidDeviceGenerator.getDeviceID(configuration.getApplicationContext()));
//        params.setTicket(engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getPayChannel(getBaseHttpPostUrl(), params, new HttpEngineInterceptor(handler));
    }

    @Deprecated
    public int createOrder(PayType type, Goods goods, SimpleAsyncResponseHandler handler) {
        final int payType = type == null ? PayType.UNKNOWN.getStartIndex() : type.getStartIndex();
        return createOrder(payType, goods, handler);
    }

    public int createOrder(int payType, Goods goods, SimpleAsyncResponseHandler handler) {
        checkEngineInitialization();

        CreateOrderRequestParams params = new CreateOrderRequestParams(payType, goods);

        setExtendedParams(params);
//        params.setUserId(engineConduits.readCurrentLoginUserUID());
//        params.setGameId(configuration.getGameId());
//        params.setTicket(engineConduits.readCurrentLoginUserTicket());
//        params.setDeviceId(AndroidDeviceGenerator.getDeviceID(configuration.getApplicationContext()));
        return httpEngine.createOrder(getBaseHttpPostUrl(), params, new HttpEngineInterceptor(handler));
    }

//    public int checkVersion(SimpleAsyncResponseHandler handler){
//        checkEngineInitialization();
//
//        CheckVersionRequestParams params = new CheckVersionRequestParams();
//        setExtendedParams(params);
////        params.setGameId(configuration.getGameId());
////        params.setDeviceId(AndroidDeviceGenerator.getDeviceID(configuration.getApplicationContext()));
//        params.setVersionName(XmlUtils.getChannelGameVersionCode());
//        return httpEngine.checkVersion(getBaseHttpPostUrl(), params, new HttpEngineInterceptor(handler));
//    }
    private RoleInfo roleInfo;
    public int uploadRoleInfo(RoleInfo roleInfo, RoleStatus status, SimpleAsyncResponseHandler handler) {
        checkEngineInitialization();
        this.roleInfo = roleInfo;
        RoleInfoRequestParams params = new RoleInfoRequestParams(roleInfo,status);

        setExtendedParams(params);
//        params.setUserId(engineConduits.readCurrentLoginUserUID());
//        params.setTicket(engineConduits.readCurrentLoginUserTicket());
//        params.setDeviceId(AndroidDeviceGenerator.getDeviceID(configuration.getApplicationContext()));
        return httpEngine.uploadRoleInfo(getBaseHttpPostUrl(), params, new HttpEngineInterceptor(handler));
    }


    public int getFloatNotice(SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        FloatCommonReuqestParams params = new FloatCommonReuqestParams("game_notice");
        params.setGameId(configuration.getGameId());
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }
    public int getFloatActivity(SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        FloatCommonReuqestParams params = new FloatCommonReuqestParams("game_activity");
        params.setGameId(configuration.getGameId());
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }

    public int getFloatGonlue(SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        FloatCommonReuqestParams params = new FloatCommonReuqestParams("gaming_strategy");
        params.setGameId(configuration.getGameId());
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }

    public int getFloatGift(SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        FloatCommonReuqestParams params = new FloatCommonReuqestParams("game_card");
        params.setGameId(configuration.getGameId());
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }
    public int floatGiftRecive(String cardId,SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        FloatGiftReciveRequestParams params = new FloatGiftReciveRequestParams();
        params.setCardId(cardId);
        params.setGameId(configuration.getGameId());
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }

    public int getFloatGameServer(SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        FloatCommonReuqestParams params = new FloatCommonReuqestParams("game_server");
        params.setGameId(configuration.getGameId());
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }
    public int getUserInfo(SimpleAsyncResponseHandler handler){
        RoleInfo info = FBSDK.getInstance().getmRoleInfo();
        checkEngineInitialization();
        FloatCommonReuqestParams params = new FloatCommonReuqestParams("user_info");
        params.setGameId(configuration.getGameId());
        params.setServer_id(info==null?"":info.getServerId());
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }
    public int getKefuInfo(SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        FloatCommonReuqestParams params = new FloatCommonReuqestParams("customer");
        params.setGameId(configuration.getGameId());
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }
    public int changePwdByPwd(String oldPwd,String newPwd,SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        FloatChangePwdByPwdParams params = new FloatChangePwdByPwdParams("");
        params.setPassword(oldPwd);
        params.setNew_password(newPwd);
        params.setGameId(configuration.getGameId());
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }

    public int changePwdByPhone(String phone,String code,String newPwd,SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        FloatChangePwdByPhoneParams params = new FloatChangePwdByPhoneParams("");
        params.setPassword(newPwd);
        params.setPhone(phone);
        params.setCode(code);
        params.setGameId(configuration.getGameId());
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }
    public int changePhone(String oldCode,String newCode,String newPhone,SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        FloatChangePhoneParams params = new FloatChangePhoneParams("");
        params.setPhone(newPhone);
        params.setCode(newCode);
        params.setVerify_code(oldCode);
        params.setGameId(configuration.getGameId());
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }

    public int getFloatCode(String phone,String type,SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        FloatPhoneCodeParams params = new FloatPhoneCodeParams("");
        params.setPhone(phone);
        params.setType(type);
        params.setGameId(configuration.getGameId());
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }

    public int redpackageMain(SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        RedMainRequestParams params = new RedMainRequestParams();
        params.setRoleId(roleInfo!=null?roleInfo.getRoleId():"");
        params.setGameId(configuration.getGameId());
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }

//    public int redpackageCommon(String server,SimpleAsyncResponseHandler handler){
//        checkEngineInitialization();
//        RedMainRequestParams params = new RedMainRequestParams(server);
//        params.setRoleId(roleInfo!=null?roleInfo.getRoleId():"");
//        params.setGameId(configuration.getGameId());
//        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
//        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
//    }

    public int redpackageBinded(SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        RedBindedRequestParams params = new RedBindedRequestParams();
        params.setRoleId(roleInfo!=null?roleInfo.getRoleId():"");
        params.setGameId(configuration.getGameId());
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }

    public int redpackageUserInfo(SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        RedUserInfoRequestParams params = new RedUserInfoRequestParams();
        params.setRoleId(roleInfo!=null?roleInfo.getRoleId():"");
        params.setGameId(configuration.getGameId());
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }

    public int redpackageWechat(SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        RedWechatRequestParams params = new RedWechatRequestParams();
        params.setRoleId(roleInfo!=null?roleInfo.getRoleId():"");
        params.setGameId(configuration.getGameId());
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }

    public int redpackageTixian(String money,String type,SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        RedTixianRequestParams params = new RedTixianRequestParams();
        params.setRoleId(roleInfo!=null?roleInfo.getRoleId():"");
        params.setGameId(configuration.getGameId());
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        params.setAmount(money);
        params.setType(type);
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }

    public int redpackageaAlipay(String account,String name,SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        RedAlipayRequestParams params = new RedAlipayRequestParams();
        params.setRoleId(roleInfo!=null?roleInfo.getRoleId():"");
        params.setGameId(configuration.getGameId());
        params.setAccount(account);
        params.setRealname(name);
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }

    public int redpackageGet(int page,SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        RedGetRequestParams params = new RedGetRequestParams();
        params.setRoleId(roleInfo!=null?roleInfo.getRoleId():"");
        params.setGameId(configuration.getGameId());
        String ttChannel = FBSDK.getInstance().getTouTiaoChannel();
        if(TextUtils.isEmpty(ttChannel)){
            params.setChannelId(XmlUtils.getSdkChannelId());
        }else {
            params.setChannelId(ttChannel);
        }
        params.setPage(page+"");
        params.setCount(10+"");
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }

    public int redpackageDetail(int page,SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        RedDetailRequestParams params = new RedDetailRequestParams();
        params.setRoleId(roleInfo!=null?roleInfo.getRoleId():"");
        params.setGameId(configuration.getGameId());
        String ttChannel = FBSDK.getInstance().getTouTiaoChannel();
        if(TextUtils.isEmpty(ttChannel)){
            params.setChannelId(XmlUtils.getSdkChannelId());
        }else {
            params.setChannelId(ttChannel);
        }
        params.setPage(page+"");
        params.setCount(10+"");
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }

    public int redpackageOpen(String id,SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        RedOpenRequestParams params = new RedOpenRequestParams();
        params.setRoleId(roleInfo!=null?roleInfo.getRoleId():"");
        params.setGameId(configuration.getGameId());
        String ttChannel = FBSDK.getInstance().getTouTiaoChannel();
        if(TextUtils.isEmpty(ttChannel)){
            params.setChannelId(XmlUtils.getSdkChannelId());
        }else {
            params.setChannelId(ttChannel);
        }
        params.setRedId(id);
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }

    public int redpackageInfo(String id,SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        RedInfoRequestParams params = new RedInfoRequestParams();
        params.setRoleId(roleInfo!=null?roleInfo.getRoleId():"");
        params.setGameId(configuration.getGameId());
        String ttChannel = FBSDK.getInstance().getTouTiaoChannel();
        if(TextUtils.isEmpty(ttChannel)){
            params.setChannelId(XmlUtils.getSdkChannelId());
        }else {
            params.setChannelId(ttChannel);
        }
        params.setRedId(id);
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }

    public int redpackageDelete(String id,SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        RedBindedDeleteRequestParams params = new RedBindedDeleteRequestParams();
        params.setRoleId(roleInfo!=null?roleInfo.getRoleId():"");
        params.setGameId(configuration.getGameId());
        String ttChannel = FBSDK.getInstance().getTouTiaoChannel();
        if(TextUtils.isEmpty(ttChannel)){
            params.setChannelId(XmlUtils.getSdkChannelId());
        }else {
            params.setChannelId(ttChannel);
        }
        params.setId(id);
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }

    public int redpackageSend(String amount,String count,int type,SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        RedSendRequestParams params = new RedSendRequestParams();
        params.setRoleId(roleInfo!=null?roleInfo.getRoleId():"");
        params.setGameId(configuration.getGameId());
        String ttChannel = FBSDK.getInstance().getTouTiaoChannel();
        if(TextUtils.isEmpty(ttChannel)){
            params.setChannelId(XmlUtils.getSdkChannelId());
        }else {
            params.setChannelId(ttChannel);
        }
        params.setAmount(amount);
        params.setCount(count);
        params.setType(type==1?"lucky":"normal");
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }

    public int redpackageCheck(String orderId,SimpleAsyncResponseHandler handler){
        checkEngineInitialization();
        RedCheckRequestParams params = new RedCheckRequestParams();
        params.setRoleId(roleInfo!=null?roleInfo.getRoleId():"");
        params.setGameId(configuration.getGameId());
        String ttChannel = FBSDK.getInstance().getTouTiaoChannel();
        if(TextUtils.isEmpty(ttChannel)){
            params.setChannelId(XmlUtils.getSdkChannelId());
        }else {
            params.setChannelId(ttChannel);
        }
        params.setOrderNo(orderId);
        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
    }
//    public int getFloatPhone(SimpleAsyncResponseHandler handler){
//        checkEngineInitialization();
//        FloatCodeReuqestParams params = new FloatCodeReuqestParams();
//        params
//        params.setGameId(configuration.getGameId());
//        params.setToken(TextUtils.isEmpty(engineConduits.readCurrentLoginUserTicket())?" ":engineConduits.readCurrentLoginUserTicket());
//        return httpEngine.getFloatCommon(getBaseHttpPostUrl(),params,new HttpEngineInterceptor(handler));
//    }

//    public int getGameActivityInfo(SimpleAsyncResponseHandler handler) {
//        checkEngineInitialization();
//
//        ActivityRequestParams params = new ActivityRequestParams(configuration.getGameId());
//        setExtendedParams(params);
////        params.setUserId(engineConduits.readCurrentLoginUserUID());
////        params.setTicket(engineConduits.readCurrentLoginUserTicket());
////        params.setDeviceId(AndroidDeviceGenerator.getDeviceID(configuration.getApplicationContext()));
//        return httpEngine.getGameActivityInfo(getBaseHttpPostUrl(), params, new HttpEngineInterceptor(handler));
//    }



//    public int getGamePushInfo(RoleInfo roleInfo, SimpleAsyncResponseHandler handler) {
//        checkEngineInitialization();
//
//        PushInfoRequestParams params = new PushInfoRequestParams();
//        setExtendedParams(params);
////        params.setGameId(configuration.getGameId());
////        params.setUserId(engineConduits.readCurrentLoginUserUID());
////        params.setTicket(engineConduits.readCurrentLoginUserTicket());
////        params.setDeviceId(AndroidDeviceGenerator.getDeviceID(configuration.getApplicationContext()));
//        params.setLastTime(engineConduits.readLastPushTime());
//        params.setRoleId(roleInfo.getRoleId());
//        params.setServerId(roleInfo.getServerId());
//        return httpEngine.getGamePushInfo(getBaseHttpPostUrl(), params, new HttpEngineInterceptor(handler));
//    }

//    public int reportVideoReward(String advertId, boolean isPlayCompleted, boolean isOwnReward) {
//        checkEngineInitialization();
//        RewardVideoRequestParams params = new RewardVideoRequestParams();
//        setExtendedParams(params);
////        params.setGameId(configuration.getGameId());
////        params.setUserId(engineConduits.readCurrentLoginUserUID());
//        params.setAdvertId(advertId);
//        params.setVideoCompleted(isPlayCompleted ? 1 : 0);
//        params.setVideoReward(isOwnReward ? 1 : 0);
//        return httpEngine.reportVideoReward(getBaseHttpPostUrl(), params, new HttpEngineInterceptor(null));
//    }


    public String getFloatMenuUrl(String key) {
        GetRequestParams.Builder builder = new GetRequestParams.Builder();
        builder.setService(key).setGameId(configuration.getGameId()).setUid(engineConduits.readCurrentLoginUserUID())
                .setTicket(engineConduits.readCurrentLoginUserTicket());
        GetRequestParams params = builder.build();
        return params.toParamsString(getBaseHttpGetUrl());
    }


    public String getUserHttpRequestUrl() {
        GetRequestParams.Builder builder = new GetRequestParams.Builder();
        builder.setService("user").setGameId(configuration.getGameId()).setUid(engineConduits.readCurrentLoginUserUID())
                .setTicket(engineConduits.readCurrentLoginUserTicket());
        GetRequestParams params = builder.build();
        return params.toParamsString(getBaseHttpGetUrl());
    }
    public String getActivityHttpRequestUrl() {
        GetRequestParams.Builder builder = new GetRequestParams.Builder();
        builder.setService("activity").setGameId(configuration.getGameId()).setUid(engineConduits.readCurrentLoginUserUID())
                .setTicket(engineConduits.readCurrentLoginUserTicket());
        GetRequestParams params = builder.build();
        return params.toParamsString(getBaseHttpGetUrl());
    }

    public String getCustomServiceHttpRequestUrl() {
        GetRequestParams.Builder builder = new GetRequestParams.Builder();
        builder.setService("customer").setGameId(configuration.getGameId()).setUid(engineConduits.readCurrentLoginUserUID())
                .setTicket(engineConduits.readCurrentLoginUserTicket());
        GetRequestParams params = builder.build();
        return params.toParamsString(getBaseHttpGetUrl());
    }

    public String getGiftsHttpRequestUrl() {
        GetRequestParams.Builder builder = new GetRequestParams.Builder();
        builder.setService("card").setGameId(configuration.getGameId()).setUid(engineConduits.readCurrentLoginUserUID())
                .setTicket(engineConduits.readCurrentLoginUserTicket());
        GetRequestParams params = builder.build();
        return params.toParamsString(getBaseHttpGetUrl());
    }

    public String getStrategyHttpRequestUrl() {
        GetRequestParams.Builder builder = new GetRequestParams.Builder();
        builder.setService("introduction").setGameId(configuration.getGameId()).setUid(engineConduits.readCurrentLoginUserUID())
                .setTicket(engineConduits.readCurrentLoginUserTicket());
        GetRequestParams params = builder.build();
        return params.toParamsString(getBaseHttpGetUrl());
    }
}
